#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QLabel>
#include <QMessageBox>

#include <Python.h>
#include <QLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QTextEdit>
PyObject *pModule = nullptr;
PyObject *pFunRead = nullptr;
PyObject *pReturn = nullptr;

Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) {
  ui->setupUi(this);

  Py_Initialize();
  if (!Py_IsInitialized()) {
    QMessageBox::warning(this, tr("严重错误"), tr("Python初始化错误！"));
    return;
  }
  PyRun_SimpleString("import sys");
  PyRun_SimpleString("sys.path.append('./')");
  pModule = PyImport_ImportModule("xmlrpclient");
  if (!pModule) {
    QMessageBox::warning(this, tr("ERROR"), tr("Can't find Module"));
    return;
  }

  pFunRead = PyObject_GetAttrString(pModule, "read");
  if (!pFunRead || !PyCallable_Check(pFunRead)) {
    QMessageBox::warning(this, tr("ERROR"), tr("Can't find Func"));
    return;
  }

  maxSize=31;
  maxX=300;
  maxY=2;


  QHBoxLayout *mainLayout = new QHBoxLayout(this);
  QVBoxLayout *buttonLayout = new QVBoxLayout();
  QPushButton *start = new QPushButton(tr("Start"));
  QPushButton *stop = new QPushButton(tr("Stop"));

  connect(start, &QPushButton::clicked, &timer,
          static_cast<void (QTimer::*)()>(&QTimer::start));
  connect(stop, &QPushButton::clicked, &timer, &QTimer::stop);

  buttonLayout->addWidget(start);
  buttonLayout->addWidget(stop);

  QHBoxLayout *intervalLayout=new QHBoxLayout;

  QLabel *label=new QLabel(this);
  label->setText(tr("Interval"));
  intervalLayout->addWidget(label);

  QLineEdit *edit=new QLineEdit(this);
  edit->setValidator(new QIntValidator);
  intervalLayout->addWidget(edit);

  buttonLayout->addLayout(intervalLayout);

  QPushButton *changeInterval=new QPushButton(this);
  changeInterval->setText(tr("Chang Interval"));
  connect(changeInterval,&QPushButton::clicked,[this,edit]{this->timer.setInterval(edit->text().toInt());});
  buttonLayout->addWidget(changeInterval);




  splineseries=new QtCharts::QSplineSeries();
  scatterSeries= new QtCharts::QScatterSeries();
  scatterSeries->setMarkerSize(8);

  chart = new QtCharts::QChart();
  chart->legend()->hide();
  chart->addSeries(scatterSeries);
  chart->addSeries(splineseries);
  chart->setTitle(tr("pople"));
  chart->createDefaultAxes();
  chart->axisY()->setRange(0,maxY);
  chart->axisX()->setRange(0,maxX);

  chartView = new QtCharts::QChartView(chart);
  chartView->setRenderHint(QPainter::Antialiasing);

  mainLayout->addWidget(chartView);

  mainLayout->addLayout(buttonLayout);
  mainLayout->setStretchFactor(buttonLayout,0);
  mainLayout->setStretchFactor(chartView,1);

  setLayout(mainLayout);

  timer.setInterval(500);
  connect(&timer, &QTimer::timeout, this, &Widget::timeout_readData);
}

Widget::~Widget() {
  delete ui;
  PyObject_CallFunction(pFunRead, nullptr);
  Py_Finalize();
}

void Widget::timeout_readData() {
  pReturn = PyEval_CallObject(pFunRead, NULL);

  int result = 888;
  if (PyArg_Parse(pReturn, "i", &result)) {
    qDebug() << result;
    data<<result;
    while(data.size()>maxSize){
        data.removeFirst();
    }
    if(isVisible()){
        splineseries->clear();
        scatterSeries->clear();
        int dx=maxX/(maxSize-1);
        int less=maxSize-data.size();
        for(int i=0;i!=data.size();++i){
            splineseries->append(less*dx+i*dx,data.at(i));
            scatterSeries->append(less*dx+i*dx,data.at(i));
        }
    }
  } else {
    qDebug() << "shibai";
  }

  // TODO a better way to update the line?
}
